package com.bdqn.controller;

import com.bdqn.dao.ListMapper;
import com.bdqn.dao.PhoneKcMapper;
import com.bdqn.pojo.UserPojo;
import com.bdqn.util.OSSUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Controller
public class ListContorller {

    @Resource
    ListMapper dao;

    @RequestMapping("/updateAvatar")
    @ResponseBody
    @CrossOrigin
    public int updateHeadPortrait(String userId, MultipartFile file, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin","*");
        OSSUtil oss = new OSSUtil();
        UserPojo pojo = dao.getuser(userId);
        String avatar = pojo.getAvatar();
        if (!avatar.equals("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png")){
            oss.delete(avatar);
        }
        String str = oss.uploadImage(file);
        int result = dao.updateHeadProtrait(str,userId);
//        System.out.println(str);
        return 0;
    }
}
